package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.TimeUtils;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityLocalPlayBinding;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;

public class LocalPlayActivity extends BaseAc<ActivityLocalPlayBinding> {

    public static String videoPath="";
    private long videoLength;
    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.tvPlayLeftText.setText(TimeUtil.getMmss(mDataBinding.videoView.getCurrentPosition()));
            mDataBinding.sbPlay.setProgress(Integer.parseInt(TimeUtils.millis2String(mDataBinding.videoView.getCurrentPosition(), "ss")));
            mHandler.postDelayed(this, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_local_play;
    }

    @Override
    protected void initView() {
        mDataBinding.ivLocalPlayBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivPlay.setOnClickListener(this);
        mDataBinding.ivLocalPlayShare.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivPlay://暂停/播放
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivPlay.setImageResource(R.drawable.bofang1);
                    mDataBinding.videoView.pause();
                    stopTime();
                } else {
                    mDataBinding.ivPlay.setImageResource(R.drawable.zanting1);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
            case R.id.ivLocalPlayShare:
                IntentUtil.shareVideo(mContext,videoPath);
                break;
        }
    }

    @Override
    protected void initData() {
        if (videoPath.isEmpty()) return;
        //设置播放器
        setPlayer();
    }

    private void setPlayer() {
        mHandler = new Handler();
        mDataBinding.tvPlayLeftText.setText("00:00");
        videoLength = MediaUtil.getDuration(videoPath);
        mDataBinding.tvPlayRightText.setText(TimeUtil.getMmss(videoLength));
        mDataBinding.sbPlay.setMax(Integer.parseInt(TimeUtils.millis2String(videoLength, "ss")));
        mDataBinding.sbPlay.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mDataBinding.videoView.seekTo(seekBar.getProgress());
            }
        });
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mDataBinding.tvPlayLeftText.setText("00:00");
                mDataBinding.ivPlay.setImageResource(R.drawable.bofang1);
                mDataBinding.sbPlay.setProgress(0);
                mDataBinding.videoView.seekTo(1);
                stopTime();
            }
        });
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivPlay.setImageResource(R.drawable.zanting1);
        mDataBinding.videoView.start();
        startTime();
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDataBinding.ivPlay.setImageResource(R.drawable.bofang1);
        mDataBinding.videoView.pause();
        stopTime();
    }

}